package com.baigt.j8.lambda;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * java.util.function.Predicate
 * 什么地方在用？
 * stream.filter
 */
public class _003PredicateDemo {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 6, 4, 10, 8, 7);
        _003PredicateDemo predicateDemo = new _003PredicateDemo();
        predicateDemo.filter(list, i -> i > 3);
        System.out.println("........");
        predicateDemo.filterNegate(list, i -> i > 3);
        System.out.println("........");
        predicateDemo.filterAnd(list, i -> i > 3, integer -> integer % 2 == 0);
        System.out.println("........");
        predicateDemo.filterOr(list, i -> i > 3, integer -> integer % 2 == 0);


        System.out.println("........");
        list.stream().filter(i -> i > 3).forEach(System.out::println);
    }

    public void filter(List<Integer> list, Predicate<Integer> predicate) {
        for (Integer num : list) {
            if (predicate.test(num)) {
                System.out.println(num);
            }
        }
    }

    public void filterNegate(List<Integer> list, Predicate<Integer> predicate) {
        for (Integer num : list) {
            if (predicate.negate().test(num)) {
                System.out.println(num);
            }
        }
    }

    public void filterAnd(List<Integer> list, Predicate<Integer> predicate, Predicate<Integer> predicateAnd) {
        for (Integer num : list) {
            if (predicate.and(predicateAnd).test(num)) {
                System.out.println(num);
            }
        }
    }

    public void filterOr(List<Integer> list, Predicate<Integer> predicate, Predicate<Integer> predicateor) {
        for (Integer num : list) {
            if (predicate.or(predicateor).test(num)) {
                System.out.println(num);
            }
        }
    }

}
